package com.ltkj.file.service.impl;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.file.mapper.CommAttachmentMapper;
import com.ltkj.file.service.ICommAttachmentService;
import com.ltkj.file.service.ISysFileService;
import com.ltkj.system.api.domain.CommAttachment;
import com.ltkj.system.api.domain.CommAttachmentQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 附件Service业务层处理
 * 
 * @author chenzeen
 * @date 2023-08-30
 */
@Service
public class CommAttachmentServiceImpl implements ICommAttachmentService 
{
    @Autowired
    private CommAttachmentMapper commAttachmentMapper;

    @Autowired
    private ISysFileService sysFileService;

    @Value("${fileTemp.path}")
    public String fileTempPath;

    /**
     * 查询附件
     * 
     * @param attaId 附件主键
     * @return 附件
     */
    @Override
    public CommAttachment selectCommAttachmentByAttaId(Long attaId)
    {
        return commAttachmentMapper.selectCommAttachmentByAttaId(attaId);
    }

    /**
     * 查询附件列表
     * 
     * @param commAttachment 附件
     * @return 附件
     */
    @Override
    public List<CommAttachment> selectCommAttachmentList(CommAttachment commAttachment)
    {
        return commAttachmentMapper.selectCommAttachmentList(commAttachment);
    }

    @Override
    public List<CommAttachment> selectCommAttachmentListByInfo(CommAttachment commAttachment) {
        return commAttachmentMapper.selectCommAttachmentListByInfo(commAttachment);
    }

    /**
     * 新增附件
     * 
     * @param commAttachment 附件
     * @return 结果
     */
    @Override
    public int insertCommAttachment(CommAttachment commAttachment)
    {
        return commAttachmentMapper.insertCommAttachment(commAttachment);
    }

    /**
     * 修改附件
     * 
     * @param commAttachment 附件
     * @return 结果
     */
    @Override
    public int updateCommAttachment(CommAttachment commAttachment)
    {
        return commAttachmentMapper.updateCommAttachment(commAttachment);
    }

    /**
     * 批量删除附件
     * 
     * @param attaIds 需要删除的附件主键
     * @return 结果
     */
    @Override
    public int deleteCommAttachmentByAttaIds(Long[] attaIds)
    {
        return commAttachmentMapper.deleteCommAttachmentByAttaIds(attaIds);
    }

    /**
     * 删除附件信息
     * 
     * @param attaId 附件主键
     * @return 结果
     */
    @Override
    public int deleteCommAttachmentByAttaId(Long attaId)
    {
        return commAttachmentMapper.deleteCommAttachmentByAttaId(attaId);
    }

    @Override
    public AjaxResult updateAttaData(CommAttachment attachment, String attaIds) {
        String[] attaIdsArr = attaIds.split(",");
        for(String attaId : attaIdsArr) {
            CommAttachment db = commAttachmentMapper.selectCommAttachmentByAttaId(Long.valueOf(attaId));
            db.setInfoId(attachment.getInfoId());
            db.setInfoCode(attachment.getInfoCode());
            commAttachmentMapper.updateCommAttachment(db);
        }
        return AjaxResult.success();
    }

    @Override
    public int deletaAttaByInfo(CommAttachment attachment) {
        return commAttachmentMapper.deleteCommAttachmentByInfo(attachment);
    }


    @Override
    public List<CommAttachment> uploadAttaDataMore(List<MultipartFile> files, String zoom, CommAttachment attachment) throws Exception {
        List<CommAttachment> list = new ArrayList<>();
        for (MultipartFile file : files) {
            CommAttachment ca = uploadAttaData(file, zoom, attachment);
            list.add(ca);
        }
        return list;
    }

    @Override
    public CommAttachment uploadAttaData(MultipartFile file, String zoom, CommAttachment attachment) throws Exception {
        // 上传并返回访问地址
        String path = sysFileService.uploadFileNoDomain(file);

        String zoomPath = "";
        if("1".equals(zoom)) {
            //压缩文件,将压缩后的文件放到fileZTemp临时路径里
            File fileTemp = new File(fileTempPath+file.getOriginalFilename());
            fileTemp.mkdirs();
            File fileZTemp = new File(fileTempPath+"zoomTemp_"+file.getOriginalFilename());
            file.transferTo(fileTemp);
            ImgUtil.scale(fileTemp,fileZTemp, 0.5f);

            //将fileZTemp转MultipartFile,上传
            InputStream inputStream = new FileInputStream(fileZTemp);
            String mainNameZ = FileNameUtil.mainName(file.getOriginalFilename())+"_zoom"+"."+FileNameUtil.extName(file.getOriginalFilename());
            MultipartFile multipartFile = new MockMultipartFile(mainNameZ, mainNameZ,FileUtil.getSuffix(file.getOriginalFilename()),(byte[]) FileCopyUtils.copyToByteArray(inputStream));
            zoomPath = sysFileService.uploadFileNoDomain(multipartFile);
            //删除临时文件
            fileTemp.delete();
            fileZTemp.delete();
        }

        attachment.setAttaName(file.getOriginalFilename());
        attachment.setAttaType(file.getContentType());
        attachment.setAttaSize(String.valueOf(file.getSize()));
        attachment.setAttaPath(path);
        //如果使用FastDfs,将@Primary从LocalSysFileServiceImpl移到FastDfsSysFileServiceImpl,赋值atta_oss_path
//            attachment.setAttaOssPath(path);
        attachment.setAttaZoomPath(zoomPath);
//        attachment.setUploadUser(SecurityUtils.getLoginUser().getUsername());
        commAttachmentMapper.insertCommAttachment(attachment);
        return attachment;
    }

    /**
     * 根据多条业务id和一个code查询附件
     * @param commAttachmentQueryVo
     * @return
     */
    @Override
    public List<CommAttachment> selectAttaByInfoIds(CommAttachmentQueryVo commAttachmentQueryVo) {
        return commAttachmentMapper.selectAttaByInfoIds(commAttachmentQueryVo);
    }
}
